/*
 * StorageChannelThread.h
 *
 *  Created on: 2017年7月5日
 *      Author: donna
 */

#ifndef STORAGECHANNELTHREAD_H_
#define STORAGECHANNELTHREAD_H_

#include "BasicType.h"
#include "MStorageType.h"
#include "MediaWriter.h"
#include "SharedPtr.h"
#include "TCriticalSection.h"
#include "DataStore.h"
#include "MediaStorage.h"
#include "MediaFormat.h"
#include "TThread.h"
#include "TEvent.h"
#include <deque>


namespace av
{

typedef std::shared_ptr< MediaWriter >	MediaWriterPtr;


class StorageChannelThread : public comn::Thread
{
public:
	StorageChannelThread();
	virtual ~StorageChannelThread();

	bool open();
	void close();

	void setSink(MediaStorageSink* sink);

	void setDataStore(DataStorePtr& dataStore);

	void postStorageEvent(StorageEvent& storageEvent);

	void postRecord(MRecord& record, bool isAdd);

	void postWriter(MediaWriterPtr& writer);


protected:
	virtual int run();
	virtual void doStop();

	void handleWriter();
	void handleRecord();
	void handleStorageEvent();

protected:
	struct RecordEntry
	{
		MRecord record;
		bool isAdd;

		RecordEntry():
			record(),
			isAdd()
		{
		}
	};

	typedef std::deque< StorageEvent >		StorageEventArray;
	typedef std::deque< RecordEntry >		MRecordArray;
	typedef std::deque< MediaWriterPtr >	MediaWriterArray;

protected:
	comn::Event	m_event;
	comn::CriticalSection m_cs;

	StorageEventArray	m_events;
	MRecordArray	m_records;
	MediaWriterArray	m_writers;

	DataStorePtr	m_dataStore;
	MediaStorageSink*	m_sink;

};

} /* namespace av */

#endif /* STORAGECHANNELTHREAD_H_ */
